export class Node {
    constructor(val, left, right) {
        this.val = val;
        this.left = left;
        this.right = right;
    };
};

const root = new Node(0, new Node(1, new Node(3), new Node(4)), new Node(2, new Node(5), new Node(6)));

// 前序遍历：0 1 3 4 2 5 6
export const preTranverse = function (root) {
    const stack = [root];
    const queue = [];
    while (stack.length) {
        const c = stack.pop();
        queue.push(c.val);
        if (c.right) stack.push(c.right);
        if (c.left) stack.push(c.left);
    }
    console.log(`PreTranverse Result:${queue.join(' ')}`);
    return queue;
};

// 中序遍历：3 1 4 0 5 2 6
export const midTranverse = function (root) {
    const stack = [];
    const queue = [];
    let c = root;
    while (stack.length || c) {
        while (c) {
            stack.push(c);
            c = c.left;
        }
        c = stack.pop();
        queue.push(c.val);
        c = c.right;
    }
    console.log(`MidTranverse Result:${queue.join(' ')}`);
    return queue;
};

// 后序遍历：3 4 1 5 6 2 0
export const postTranverse = function (root) {
    const stack = [root];
    const queue = [];
    while (stack.length) {
        const c = stack.pop();
        queue.unshift(c.val);
        if (c.left) stack.push(c.left);
        if (c.right) stack.push(c.right);
    }
    console.log(`PostTranverse Result:${queue.join(' ')}`);
    return queue;
};

preTranverse(root);
midTranverse(root);
postTranverse(root);